<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            刀具报废
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">刀具序列号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="扫描/录入刀具序列号" id="toolSeqence" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">报废责任</div>
                    <select id="duty">
                    </select>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">报废描述</div>
                    <div class="aui-list-item-input">
                        <textarea id="description" onclick="getIdSelector();"></textarea>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">原因分析</div>
                    <div class="aui-list-item-input">
                        <textarea id="analysis" onclick="getIdSelector();"></textarea>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">备注</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="remark" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">报废时间</div>
                    <div class="aui-list-item-input" onclick="timePicker();">
                        <input type="text" placeholder="选择时间" class="aui-input" id="scrapDate">
                    </div>
                </div>
            </li>
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="save()">保存</div>
        <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;">
            <table class="table" style='width:100%;' id="table">
                <thead>
                    <tr style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 12px;font-weight: bold;">
                        <td></td>
                        <td>序号</td>
                        <td algin='center'>报废原因编码</td>
                        <td algin='center'>报废原因名称</td>
                        <td algin='center'>备注</td>
                        <td style="display:none;">id</td>
                    </tr>
                </thead>
                <tbody id="tbodyHtml">
                </tbody>
            </table>
        </div>
    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";

    //初始化
    apiready = function() {

        $("#toolSeqence").focus();
        idSelector = "toolSeqence";

        //初始化当前日期
        crateDate();

        //查询责任下拉框
        getCombox();

        //查询报废原因
        doSearch();
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //获取当前日期
    function crateDate() {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        $("#scrapDate").val(year + "-" + month + "-" + day);
    }

    //时间选择器
    function timePicker() {
        api.openPicker({
            type: 'date',
            date: new Date(),
            title: '选择时间'
        }, function(ret, err) {
            if (ret) {
                var monthTemp = (ret.month < 10 ? '0' + ret.month : ret.month);
                var dayTemp = (ret.day < 10 ? '0' + ret.day : ret.day);
                var date = ret.year + "-" + monthTemp + "-" + dayTemp;
                $("#scrapDate").val(date);
            }
        });
    }

    //查询生产线，班次下拉框
    function getCombox() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getMdparaconfig";
        var data = {
            values: {
                paracls: 'duty',
                un:un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == "0") {

                    //生产线
                    var list = eval(myobj.list);
                    var html = "<option value=''>--请选择--</option>";
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].code + "'>" + list[i].name + "</option>";
                    }
                    $api.html($api.byId("duty"), html);

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //查询报废原因
    function doSearch() {

        //刀具唯一号
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/doSearchScrapReason";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == "0") {

                    var list = eval(myobj.list);
                    var html = "";
                    for (var i = 0; i < list.length; i++) {

                        //只可以修改未退货的数据
                        html += "<tr style='text-align: center;background-color: #F5F5F5;color: black;font-size: 10px;'>";
                        html += "<td><input name='sub' type='checkbox' /></td>";
                        html += "<td name='seqno' style='width:40px;'>" + (i + 1) + "</td>";
                        html += "<td name='scrapcauseCode'>" + list[i].scrapcauseCode + "</td>";
                        html += "<td name='scrapcauseName'>" + list[i].scrapcauseName + "</td>";
                        html += "<td name='remark'>" + list[i].remark + "</td>";
                        html += "<td name='id' style='display:none;'>" + list[i].id + "</td>";
                        html += "</tr>";
                    }
                    $api.html($api.byId("tbodyHtml"), html);

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //报废记录保存
    function save() {

        //刀具唯一号
        var toolSeqence = $("#toolSeqence").val();
        //报废责任
        var duty = $("#duty").val();
        //报废描述
        var description = $("#description").val();
        //原因分析
        var analysis = $("#analysis").val();
        //备注
        var remark = $("#remark").val();
        //报废日期
        var scrapDate = $("#scrapDate").val();

        //刀具唯一号非空校验
        if (toolSeqence == null || toolSeqence == "" || toolSeqence == undefined) {
            api.toast({
                msg: "请录入或扫描刀具唯一号",
                duration: 3000
            });
            return;
        }

        //报废责任非空校验
        if (duty == null || duty == "" || duty == undefined) {
            api.toast({
                msg: "请选择报废责任",
                duration: 3000
            });
            return;
        }

        //报废日期非空校验
        if (scrapDate == null || scrapDate == "" || scrapDate == undefined) {
            api.toast({
                msg: "请选择报废日期",
                duration: 3000
            });
            return;
        }

        var jsonHead = "[";
        var detailJson = "";
        var jsonTail = "]";

        //获取选中报废原因数据
        var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据

        var id = "";
        var scrapcauseCode = "";
        var scrapcauseName = "";
        var remarkS = "";

        //遍历
        check.each(function() {
            var row = $(this).parent("td").parent("tr");
            id = row.find("[name='id']").html();
            scrapcauseCode = row.find("[name='scrapcauseCode']").html();
            scrapcauseName = row.find("[name='scrapcauseName']").html();
            remarkS = row.find("[name='remark']").html();

            detailJson = detailJson + "{\"id\":\"" + id + "\",\"scrapcauseCode\":\"" + scrapcauseCode +
                "\",\"scrapcauseName\":\"" + scrapcauseName + "\",\"remark\":\"" + remarkS + "\"},";
        });

        if (detailJson != null && detailJson != "" && detailJson != undefined) {
            detailJson = detailJson.substring(0, detailJson.length - 1);
            detailJson = jsonHead + detailJson + jsonTail;
        }

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/toolScrapSave";
        var data = {
            values: {
                toolSeqence: toolSeqence,
                duty: duty,
                description: description,
                analysis: analysis,
                remark: remark,
                scrapDate: scrapDate,
                detailJson: detailJson,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {

                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });

                    //清空表单
                    $("#toolSeqence").val("");
                    $("#duty").val("");
                    $("#description").val("");
                    $("#analysis").val("");
                    $("#remark").val("");

                    //失败，存在没有订单数据
                } else if (myobj.code == "1") {

                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function(ret, err) {

            //赋值
            $("#" + idSelector).val(ret);

        });
    }

    //刀具序列号回车事件
    $("#toolSeqence").on('keypress', function(e) {
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            $("#toolSeqence").val(sm);

        }
    });

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>
